# 圆扇形
import math

import pygame.gfxdraw
import pygame

from pygame_test.Arc import Arc


class SectorGfx:
    def __init__(self, x, y, radius, color, begin, end, width=0):
        self.x = x  # 中心点x
        self.y = y  # 中心点x
        self.radius = radius
        self.color = color
        self.rec_rect = None
        # 变成360度以内的角
        if begin != 360:
            begin %= 360
        if end != 360:
            end %= 360
        # 如果开始角度大于结束角度而且角度差小于半圈, 交换两者
        # if begin > end and begin - end <= 180:
        #     begin, end = end, begin

        self.begin = begin
        self.end = end
        self.width = width

    def draw(self, surface):
        # 扇形的边缘宽度
        width = self.width
        if self.width == 0 or self.width > self.radius:
            width = self.radius
        x = self.x
        y = self.y
        # w = 0
        # h = 0
        x_moves = []
        y_moves = []

        for i in range(self.radius - width, self.radius + 1):
            # self.x self.y是弧形的原点, 变成rect时要向左上移动, 所以x和y都要减去i
            # 因为给的是半径 所以rect的w和h要×2
            rect = pygame.Rect(self.x - i, self.y - i, 2 * i, 2 * i)
            # print(i, rect)
            # arc = Arc(rect, self.color, self.begin, self.end, 1)

            pygame.gfxdraw.arc(surface, self.x, self.y, i,self.begin,self.end,self.color)

        #     xx, yy, ww, hh = arc.draw(surface)
        #     x_moves.append(xx)
        #     y_moves.append(yy)
        #     x = min(x, xx)
        #     y = min(y, yy)
        # # 蓝框, 最后(最大)一个弧形画完后,形成的rect
        # # pygame.draw.rect(surface, (51, 201, 255), (xx, yy, ww, hh), 2)
        # x_move = x_moves[len(x_moves) - 1] - x_moves[0]
        # y_move = y_moves[len(y_moves) - 1] - self.y
        # # 角度在90以内
        # if 0<self.end - self.begin <= 90:
        #     # 起点和终点角度都在同一个象限内
        #     if 0 <= self.begin <= 90 and 0 <= self.end <= 90:
        #         w = (x_move) + ww
        #         h = abs(y_move)
        #     elif 90 <= self.begin <= 180 and 90 <= self.end <= 180:
        #         w = abs(x_move)
        #         h = abs(y_move)
        #     elif 180 <= self.begin <= 270 and 180 <= self.end <= 270:
        #         w = abs(x_move)
        #         h = abs(y_move) + hh
        #     elif 270 <= self.begin <= 360 and 270 <= self.end <= 360:
        #         w = abs(x_move) + ww
        #         h = abs(y_move) + hh
        #     # 起点和终点角度不在同一个象限内
        #     elif 0 <= self.begin < 90:
        #         w = ww
        #         h = abs(y_move)
        #     elif 90 <= self.begin < 180:
        #         w = self.x - x
        #         h = hh
        #     elif 180 <= self.begin < 270:
        #         w = ww
        #         h = abs(y_move) + hh
        #     elif 180 <= self.begin < 360:
        #         w = abs(x_move) + ww
        #         h = hh
        # elif self.end - self.begin > 180:
        #     w = ww
        #     h = hh
        # elif 90 < self.end - self.begin <= 180:
        #     if 0 <= self.begin < 90:
        #         w = ww
        #         h = self.radius
        #     elif 90 <= self.begin < 180:
        #         w = self.x - x
        #         h = hh
        #     elif 180 <= self.begin < 270:
        #         w = ww
        #         h = abs(y_move) + hh
        # # elif self.begin>self.end:
        # else:
        #     # 逆时针转
        #     w = ww
        #     h = hh
        #
        # self.arc_rect = pygame.Rect(x, y, w, h)
        # # 红框, 扇形画好后形成的rect
        # # pygame.draw.rect(surface, (255, 2, 1), self.arc_rect, 1)

    def get_rect(self):
        return self.arc_rect

    # 角度to弧度
    def __angle_to_arc(self, angle):
        import math
        a = angle % 360 * math.pi / 180
        return a


if __name__ == '__main__':
    import sys

    pygame.init()
    screen = pygame.display.set_mode((200, 200))
    fps = pygame.time.Clock()
    # todo begin=360时有问题
    begin = 1
    end = 15
    arc = Sector(100, 100, 60, (255, 255, 255), begin, end)
    arc.draw(screen)
    while True:
        # screen.fill((0, 0, 0))
        for e in pygame.event.get():
            if e.type == pygame.QUIT:
                pygame.quit()
                sys.exit()
            elif e.type == pygame.KEYDOWN:
                if e.key == pygame.K_ESCAPE:
                    pygame.quit()
                    sys.exit()

        pygame.display.update()
        fps.tick(1)
